package controllers.wechatlab;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
//import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.ModelAndView;

import cn.edu.bjtu.alex.hotswap.TextCateClassLoader;
import cn.edu.bjtu.alex.rewrite.Context;
import cn.edu.bjtu.alex.rewrite.TrainDriver;
import cn.edu.bjtu.alex.rewrite.TransformDriver;
import cn.edu.bjtu.alex.rewrite.components.TransformOutputing;
import dao.BaseDao;
import dao.wechatlab.WechatClassMapDao;
import pojo.weichatlab.Label;
import pojo.weichatlab.model.Document;
import service.wechatlab.impl.CompoundTestService;
import thirdpart.wechat.rmi.WechatInterface;
import thirdpart.wechat.rmi.WechatService;

@Controller
@RequestMapping(value="test")
	
public class TestController extends WechatBaseController{
	@Autowired CompoundTestService cts;
	
	ClassLoader cl = new TextCateClassLoader();
	
	@RequestMapping(value="/hotswap.do")
	public void hotswap(HttpServletRequest req,HttpServletResponse res) throws IOException{
		System.out.println("");
		try {
			Class clzz = cl.loadClass("cn.edu.bjtu.alex.hotswap.A");
			Object o = clzz.newInstance();
			System.out.println(o.getClass().getClassLoader());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		};
		res.getOutputStream().println("test ok no exception");
	}
	
//	@RequestMapping(value="/testdb.do")
	public void testdb(HttpServletRequest req,HttpServletResponse res) throws IOException{
		//System.out.println(req.getSession().getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE));
		int id = Integer.parseInt(req.getParameter("id"));
		cts.insertTest(id);
		res.getWriter().print("hehe");
		
	}

	@RequestMapping(value="/getPath.do")
	public void path(HttpServletRequest req,HttpServletResponse res) throws IOException{
		String s = this.wac.getServletContext().getRealPath("/");
		res.getWriter().println(s);
	}
	@RequestMapping(value="/test2.do")
	public void test2(HttpServletRequest req,HttpServletResponse res) throws IOException{
		res.getWriter().println("asdfsafsafsafsaf srpring in maven ");
	}
	
	
	@RequestMapping(value="/test3.do")
	public ModelAndView test3(HttpServletRequest req,HttpServletResponse res) throws IOException{
		return new ModelAndView("test");
	}
	@RequestMapping(value="/test4.do")
	@ResponseBody
	public String test4(HttpServletRequest req,HttpServletResponse res) throws IOException{
		return "wowowoowowowowoow";
	}
	@RequestMapping(value="/test5.do")
	public void test5(HttpServletRequest req,HttpServletResponse res) throws IOException{
		File f = new File("/Users/zhangzhidong/Downloads/IMG_5164.PNG");
		 res.setCharacterEncoding("utf-8");
	     res.setContentType("multipart/form-data");
	     res.setHeader("Content-Disposition", "attachment;fileName="
	                + "heh.PNG");
		FileInputStream fis = new FileInputStream(f);
		byte []buff = new byte[1024*2*1024];//2M
		OutputStream out  =  res.getOutputStream();
		int count = 0;
		while((count=fis.read(buff))!=-1){
			out.write(buff,0,count);
		}
		fis.close();
		return ;
	}
	
}
